package org.example.fancystudy.LockSupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author chaigq
 * @version 1.0
 * @date 2022/11/11 10:22
 */
public class LockSupportDemo1 {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "come in");
            lock.lock();
            try {
                condition.await();
            }catch (InterruptedException e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        },"t1").start();

        try{
            TimeUnit.SECONDS.sleep(2);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        new Thread(() -> {
            lock.lock();
            condition.signal();
            lock.unlock();
            System.out.println(Thread.currentThread().getName() + "发起唤醒！");
        },"t2").start();
    }
}
